import Vue from 'vue'
import qs from 'qs'
export default{
  state: {
    isLogin: !!localStorage.isLogin,
    user: {
      username: ''
    }
  },
  mutations: {
    login (state, payload) {
      state.user.username = payload.username
      state.isLogin = true
    }
  },
  actions: {
    login (context, payload) {
      console.log('1')
      return new Promise((resolve, reject) => {
        Vue.axios.post('/api/login', qs.stringify(payload)).then(res => {
          if (res.data === 'success') {
            context.commit('login', payload)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(() => {
          let errorData = {
            message: '系统异常'
          }
          reject(errorData)
        })
      })
    }
  },
  getters: {
    getUserName (state) {
      return state.user.username
    }
  }
}
